﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EntityFrameworkTests.cs" company="Believe2014">
//   WhenYouBelieve2012@gmail.com
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Linq;
using System.Reflection;
using EntityInterfaceGenerator.Example.AdventureWorksDataAccess;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject
{
    /// <summary>
    ///     The entity framework tests.
    /// </summary>
    [TestClass]
    public class EntityFrameworkTests
    {
        /// <summary>
        ///     The smoke test.
        /// </summary>
        [TestMethod]
        public void SmokeTest()
        {
            using (var context = new AdventureWorksEntities())
            {
                if (context.Database != null) context.Database.Log = Console.Write;

                // lazy and dont want to research why this isnt loading 
                // use reflection to iterate over each collection in the context and just watch for errors.
                Type t = context.GetType();
                if (t == null) return;
                PropertyInfo[] props = t.GetProperties();
                foreach (PropertyInfo p in props)
                {
                    if (p != null)
                    {
                        bool isEnumerable = (from i in p.PropertyType.GetInterfaces()
                            where i == typeof (IEnumerable)
                            select i
                            ).Any();

                        if (!isEnumerable) continue;
                    }

                    try
                    {
                        var dataToPull = (IEnumerable) p.GetValue(context, null);
                        if (dataToPull != null)
                        {
                            object result = (from object d in dataToPull select d).FirstOrDefault();
                        }
                    }
                    catch (Exception ex)
                    {
                        string error = ex.Message;
                        if (ex.InnerException != null)
                            error += ex.InnerException.Message;
                        Assert.Fail(error);
                    }
                }
            }
        }
    }
}